package net.csdn.business.webhook.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.csdn.business.common.domain.query.WebhookLogQuery;
import net.csdn.business.common.domain.request.webhook.NewWebhookLogDTO;
import net.csdn.business.common.domain.vo.WebhookLogVO;
import net.csdn.business.common.service.SuperServiceImpl;
import net.csdn.business.webhook.entity.WebhookLog;
import net.csdn.business.webhook.mapper.WebhookLogMapper;
import net.csdn.business.webhook.service.IWebhookLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WebhookLogServiceImpl
 * @Description webhook通知日志Service业务层处理
 * @Author zhangyl
 * @Date 2023/4/23 17:18
 */
@Service
public class WebhookLogServiceImpl extends SuperServiceImpl<WebhookLogMapper, WebhookLog> implements IWebhookLogService
{

    /**
     * 分页查询webhook通知日志列表
     *
     * @param query webhook通知日志
     * @return webhook通知日志
     */
    @Override
    public Page<WebhookLogVO> findPageList(WebhookLogQuery query) {
        //获得Page对象
        Page<WebhookLogVO> page = query.getPage();
        List<WebhookLogVO> pageList=baseMapper.findPageList(page,query);
        page.setRecords(pageList);
        return page;
    }

    /**
     * 根据id查询webhook通知日志
     * 
     * @param id webhook通知日志主键
     * @return webhook通知日志
     */
    @Override
    public WebhookLogVO selectWebhookLogById(String id)
    {
        return baseMapper.selectWebhookLogById(id);
    }

    /**
     * 查询webhook通知日志列表
     * 
     * @param query webhook通知日志
     * @return webhook通知日志
     */
    @Override
    public List<WebhookLogVO> selectWebhookLogList(WebhookLogQuery query)
    {
        return baseMapper.selectWebhookLogList(query);
    }

    /**
     * 新增webhook通知日志
     *
     * @param newWebhookLogDTO webhook通知日志
     * @return 结果
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public Integer insertWebhookLog(NewWebhookLogDTO newWebhookLogDTO)
    {
        WebhookLog webhookLog=new WebhookLog();
        BeanUtils.copyProperties(newWebhookLogDTO,webhookLog);
        webhookLog.setObjectId(IdUtil.fastSimpleUUID());
        webhookLog.setCreatedDate(new Date());
        return baseMapper.insert(webhookLog);
    }

    /**
     * 删除webhook通知日志信息
     * 
     * @param id webhook通知日志主键
     * @return 结果
     */
    @Override
    public Integer deleteWebhookLogById(String id)
    {
        return baseMapper.deleteWebhookLogByWebHookId(id);
    }

    @Override
    public Map<String,Object> getLastDelivery(String webhookId) {
        Map<String,Object> result=null;
        WebhookLogVO webhookLogVO=baseMapper.selectLastWebhookLog(webhookId);
        if(webhookLogVO!=null){
            result=new HashMap<>();
            String resStatus=webhookLogVO.getResStatus();
            StringBuilder messge=new StringBuilder();
            if("200".equals(resStatus)){
                //200则最近一次请求正常
                result.put("isNormal",true);
                messge.append("Last delivery was successful.");
            }else if(StringUtils.isNotBlank(resStatus)){
                result.put("isNormal",false);
                messge.append("Last delivery was not successful.");
                messge.append("Invalid HTTP Response: ").append(resStatus).append(".");
            }else{
                String errorMessage=webhookLogVO.getInternalErrorMessage();
                result.put("isNormal",false);
                messge.append("Last delivery was not successful.");
                messge.append(errorMessage).append(".");
            }
            result.put("messge",messge.toString());
        }
        return result;
    }
}
